﻿package com.minarto.controls {
	import com.minarto.utils.GPool;
	import de.polygonal.core.ObjectPool;
	import flash.display.DisplayObject;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getDefinitionByName;
	import scaleform.clik.controls.*;
	
	
    public class ButtonBarX extends ButtonBar {
		protected var _lineRenderers:Vector.<DisplayObject> = new <DisplayObject>[],
						_lineRendererClass:Class;
		
		
		protected var _paddingLeft:int = 0, _maxWidth:Number;
		
		
        [Inspectable(defaultValue="false")]
        public function get paddingLeft():int { return _paddingLeft; }
        public function set paddingLeft($v:int):void {
            if (_paddingLeft == $v) { return; }
            _paddingLeft = $v;
			validateNow();
        }
		
		
		[Inspectable(name="lineRenderer", defaultValue="ButtonBarXLine")]
        public function set lineRendererName(value:String):void {
            if (!value) { return; }
            // Need a try/catch in case the specified class cannot be found:
            try { 
                var classRef:Class = getDefinitionByName(value) as Class;
            } catch (error:*) {
                throw new Error("The class " + value + " cannot be found in your library. Please ensure it exists.");
            }
			            
			_lineRendererClass = classRef;
            if (classRef) {
				invalidate();
            }
        }
		
		
		override protected function initialize():void {
            super.initialize();
            _lineRenderers.length = 0;
			
			var d:DisplayObject = getChildByName("guide");
			if (d) {
				_maxWidth = _width;
				removeChild(d);
			}
        }
		
		
		override public function toString():String {
            return "[minarto ButtonBarX " + name + "]";
        }
		
		
		override protected function updateRenderers():void {
			scaleX = 1;
			scaleY = 1;
			container.scaleX = 1;
			container.scaleY = 1;
				
			var i:uint = 0, c:uint, _x:Number, _y:Number;
			var itemP:ObjectPool, lineP:ObjectPool, d:DisplayObject, b:Button;
			
			if (!_itemRendererClass || !_lineRendererClass)	return;
			
			itemP = GPool.getPool(_itemRendererClass);
			i = _renderers.length;
			while (i --) {
				b = _renderers.pop();
				b.group.removeButton(b);
				container.removeChild(b);
				itemP.object = b;
			}
			
			lineP = GPool.getPool(_lineRendererClass);
			i = _lineRenderers.length;
			while (i --) {
				d = _lineRenderers.pop();
				container.removeChild(d);
				lineP.object = d;
			}
			
			d = lineP.object;
			d.width = _x = _paddingLeft;
			d.scaleY = 1;
			_lineRenderers.push(container.addChild(d));
			
			if (_autoSize == TextFieldAutoSize.NONE && _buttonWidth > 0) {
				var a:Boolean = true;
			}
			else if (_autoSize != TextFieldAutoSize.NONE) {
				a = false;
			}
			
			c = _dataProvider.length;
			for (i = 0; i < c; ++i) {
				b = itemP.object;
				setupRenderer(b, i);
				b.group = _group;
				b.x = _x;
				b.y = 0;
				populateRendererData(b, i);
                if (a) {
                    b.width = _buttonWidth;
                } else if (!a) {
                    b.autoSize = _autoSize;
                }
				b.validateNow();
				if (!i) {
					_y = b.height - d.height;
				}
				_x += b.width;
				_renderers.push(container.addChild(b));
				
				d = lineP.object;
				d.x = _x;
				d.y = _y;
				d.width = _spacing;
				d.scaleY = 1;
				_lineRenderers.push(container.addChild(d));
				
				_x += _spacing;
			}
			
			d.width = _maxWidth - d.x;
			
			d = _lineRenderers[0];
			d.x = 0;
			d.y = _y;
        }
	}
}